﻿// See https://aka.ms/new-console-template for more information
using Autofac;
using Autofac.Features.Indexed;


var builder = new ContainerBuilder();//创建容器

builder.RegisterType<Doge>().As<IAnimal>();//注册对象
builder.RegisterType<Cat>().As<IAnimal>();//多次注册以最后一个为准
builder.RegisterType<Pig>().Named<IAnimal>("pig");//可以通过命名进行区分
//Named和Keyed功能相同，Keyed可以使用枚举类型作为键值区分
builder.RegisterType<Cat>().Keyed<IAnimal>(AnumalType.Cat);

var container = builder.Build();//创建容器完毕

var animal = container.Resolve<IIndex<AnumalType, IAnimal>>();

var dog = container.Resolve<IAnimal>();//通过IOC容器创建对象
var pig=container.ResolveNamed<IAnimal>("pig");
var cat = animal[AnumalType.Cat];
dog.SayHello();
pig.SayHello();
cat.SayHello();


Console.WriteLine("Hello, World!");

public enum AnumalType
{
    Doge,Pig,Cat
}

public interface IAnimal
{
    public void SayHello()
    {
        Console.WriteLine("我是动物");
    }
}

public class Doge : IAnimal
{
    public void SayHello()
    {
        Console.WriteLine("我是狗");
    }
}

public class Person : IAnimal
{
    public void SayHello()
    {
        Console.WriteLine("我是人");
    }
    public string Name { get; set; }
    public int Age { get; set; }
}

public class Cat : IAnimal
{
    public void SayHello()
    {
        Console.WriteLine("我是猫");
    }
}
public class Pig:IAnimal
{
    public void SayHello()
    {
        Console.WriteLine("我是猪");
    }
}


